package io.renren.modules.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.demo.dao.DhOutdorsportaddInfoDao;
import io.renren.modules.demo.dto.DhOutdorsportaddInfoDTO;
import io.renren.modules.demo.entity.DhOutdorsportaddInfoEntity;
import io.renren.modules.demo.service.DhOutdorsportaddInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 户外活动新增信息
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-04-29
 */
@Service
public class DhOutdorsportaddInfoServiceImpl extends CrudServiceImpl<DhOutdorsportaddInfoDao, DhOutdorsportaddInfoEntity, DhOutdorsportaddInfoDTO> implements DhOutdorsportaddInfoService {

    @Autowired
    private DhOutdorsportaddInfoDao dhOutdorsportaddInfoDao;

    @Override
    public QueryWrapper<DhOutdorsportaddInfoEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<DhOutdorsportaddInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    /**
     * 根据户外运动编号获取户外运动附加信息
     *
     * @param sportId
     * @return
     */
    @Override
    public DhOutdorsportaddInfoDTO getSportAddInfo(Integer sportId) {
        LambdaQueryWrapper<DhOutdorsportaddInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(DhOutdorsportaddInfoEntity::getSportId, sportId);
        List<DhOutdorsportaddInfoEntity> list = dhOutdorsportaddInfoDao.selectList(queryWrapper);
        DhOutdorsportaddInfoDTO dhOutdorsportaddInfoDTO = new DhOutdorsportaddInfoDTO();
        for (DhOutdorsportaddInfoEntity dhOutdorsportaddInfoEntity : list) {
            dhOutdorsportaddInfoDTO.setSportId(dhOutdorsportaddInfoEntity.getSportId());
            dhOutdorsportaddInfoDTO.setSportAddress(dhOutdorsportaddInfoEntity.getSportAddress());
            dhOutdorsportaddInfoDTO.setSportLeader(dhOutdorsportaddInfoEntity.getSportLeader());
            dhOutdorsportaddInfoDTO.setSportTraffic(dhOutdorsportaddInfoEntity.getSportTraffic());
        }
        return dhOutdorsportaddInfoDTO;
    }

    /**
     * 修改户外运动附加信息
     *
     * @param dto
     */
    @Override
    @Transactional
    public void updateSportAddInfo(DhOutdorsportaddInfoDTO dto) {
        DhOutdorsportaddInfoEntity dhOutdorsportaddInfoEntity = new DhOutdorsportaddInfoEntity();
        dhOutdorsportaddInfoEntity.setSportAddress(dto.getSportAddress());
        dhOutdorsportaddInfoEntity.setSportLeader(dto.getSportLeader());
        dhOutdorsportaddInfoEntity.setSportTraffic(dto.getSportTraffic());
        LambdaQueryWrapper<DhOutdorsportaddInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(DhOutdorsportaddInfoEntity::getSportId,dto.getSportId());
        dhOutdorsportaddInfoDao.update(dhOutdorsportaddInfoEntity,queryWrapper);
    }
}